package com.tangkun.environment.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;

import com.tangkun.environment.R;

/**
 * LoadDialog - v 1.0.0
 * classes:com.yixin.oaoinventory.view.LoadDialog
 *
 * @author zhuaibing
 * @date 4/13 0013
 * @time 9:49
 * @description
 */
public class LoadDialog {
    private Dialog dialog;
    private View layout_textloading;
    private View layout_barloading;
    private TextView tv_hint;

    public LoadDialog(Context context) {
        dialog = new Dialog(context, R.style.loading_dialog);
        dialog.setContentView(R.layout.dialog_loading);
        layout_textloading = dialog.findViewById(R.id.layout_textloading);
        layout_barloading = dialog.findViewById(R.id.layout_barloading);
        tv_hint = (TextView) dialog.findViewById(R.id.tv_hint);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(true);
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dismiss();
                if (listener != null) {
                    listener.onDialongKeyDown();
                }
            }
        });
    }

    public void show(String text, boolean isReturnKeyCancel, OnDialongKeyDownListener listener) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        if (text != null && !text.equals("")) {
            layout_textloading.setVisibility(View.VISIBLE);
            layout_barloading.setVisibility(View.GONE);
            tv_hint.setText(text);
        } else {
            layout_textloading.setVisibility(View.GONE);
            layout_barloading.setVisibility(View.VISIBLE);
        }
        this.listener = listener;
        if (isReturnKeyCancel) {
            dialog.setCancelable(true);
        } else {
            dialog.setCancelable(false);
        }
        dialog.show();
    }

    public void dismiss() {
        dialog.dismiss();
    }

    private OnDialongKeyDownListener listener;

    public interface OnDialongKeyDownListener {
        public void onDialongKeyDown();
    }
}
